package com.ruoyi.invoice.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.invoice.dto.SalesInvoiceDTO;
import com.ruoyi.invoice.dto.SalesInvoiceProductsDTO;
import com.ruoyi.invoice.vo.SalesInvoiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.invoice.domain.SalesInvoice;
import com.ruoyi.invoice.service.ISalesInvoiceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 销项发票Controller
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/sales_invoice")
public class SalesInvoiceController extends BaseController {
    @Autowired
    private ISalesInvoiceService salesInvoiceService;


    /**
     * 发票种类下拉框数据   业务种类下拉框数据
     *
     * @return
     */
    @GetMapping("/getSelectAllData")
    public AjaxResult getSelectAllData() {
        return success(salesInvoiceService.getSelectAllData());
    }


    /**
     * 查询销项发票列表
     */
    @RequiresPermissions("invoice:sales_invoice:list")
    @GetMapping("/list")
    public TableDataInfo list(SalesInvoiceDTO salesInvoiceDTO) {
        startPage();
        List<SalesInvoiceVO> list = salesInvoiceService.selectSalesInvoiceList(salesInvoiceDTO);
        return getDataTable(list);
    }


    /**
     * 获取销项发票详细信息
     */
    @RequiresPermissions("invoice:sales_invoice:query")
    @GetMapping(value = "/{salesInvoiceId}")
    public AjaxResult getInfo(@PathVariable("salesInvoiceId") Long salesInvoiceId) {
        return success(salesInvoiceService.selectSalesInvoiceBySalesInvoiceId(salesInvoiceId));
    }

    /**
     * 新增销项发票
     */
    @RequiresPermissions("invoice:sales_invoice:add")
    @PostMapping
    public AjaxResult add(@RequestBody SalesInvoiceProductsDTO salesInvoiceProductsDTO) {

        // 校验发票类型
        if (salesInvoiceProductsDTO.getInvoiceTypeId() == 1 || salesInvoiceProductsDTO.getInvoiceTypeId() == 2 || salesInvoiceProductsDTO.getInvoiceTypeId() == 5) {
            // 校验发票代码和发票号码
            if (salesInvoiceProductsDTO.getInvoiceCode() == null || salesInvoiceProductsDTO.getInvoiceNumber() == null) {
                throw new RuntimeException("发票代码和发票号码不能为空");
            }
            // 校验车架号和机器码和税控码
            if (salesInvoiceProductsDTO.getInvoiceTypeId() == 5 ) {
                if (salesInvoiceProductsDTO.getChassisNumber() == null || salesInvoiceProductsDTO.getMachineCode() == null || salesInvoiceProductsDTO.getTaxAuthorityCode() == null) {
                    throw new RuntimeException("车架号和机器码和税控码不能为空");
                }
            }
            // 校验发票类型
        } else if (salesInvoiceProductsDTO.getInvoiceTypeId() == 3 || salesInvoiceProductsDTO.getInvoiceTypeId() == 4) {
            if (salesInvoiceProductsDTO.getFullElectricInvoiceNumber() == null) {
                throw new RuntimeException("全电发票号码不能为空");
            }
            salesInvoiceProductsDTO.setInvoiceNumber(salesInvoiceProductsDTO.getFullElectricInvoiceNumber());
        }

        // 认证状态为认证时，认证日期不能为空
        if (salesInvoiceProductsDTO.getAuthenticationStatus() == 1 && salesInvoiceProductsDTO.getAuthenticationDate() == null) {
            throw new RuntimeException("认证状态为认证时，认证日期不能为空");
        }

        // 录入明细为 是时，明细不能少于一条
        if (salesInvoiceProductsDTO.getIsDetailEntered() == 1) {
            if (salesInvoiceProductsDTO.getDetails().get(0).getId() == null) {
                throw new RuntimeException("未录入明细");
            }
        }

        return toAjax(salesInvoiceService.insertSalesInvoice(salesInvoiceProductsDTO));
    }

    /**
     * 修改销项发票
     */
    @RequiresPermissions("invoice:sales_invoice:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SalesInvoice salesInvoice) {
        return toAjax(salesInvoiceService.updateSalesInvoice(salesInvoice));
    }

    /**
     * 删除销项发票
     */
    @RequiresPermissions("invoice:sales_invoice:remove")
    @DeleteMapping("/{salesInvoiceIds}")
    public AjaxResult remove(@PathVariable Long[] salesInvoiceIds) {
        return toAjax(salesInvoiceService.deleteSalesInvoiceBySalesInvoiceIds(salesInvoiceIds));
    }
}
